﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using TowerDefense.Units;

namespace TowerDefense.Towers
{
    public class Projectile : Munition
    {
        public Projectile()
        {
            //throw new System.NotImplementedException();
        }

        public int ProjectileSpeed
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }

        public int Width
        {
            get;
            set;
        }

        public ProjectileState CurrentState
        {
            get;
            set;
        }

        public override void Update(GameTime gameTime)
        {
            

            switch (CurrentState)
            {
                case ProjectileState.InAir:
                    this.Position = this.Position + Library.MoveDirection(this.Position, this.ProjectileSpeed, this.Direction);
                    this.Direction = Library.FindAngle(this.Position, Target.Position);
                    Vector2 delta = this.Position - Target.Position;
                    if (delta.X < 10 && delta.X > -10 && delta.Y < 10 && delta.Y > -10)
                    {
                        Impact();
                        this.CurrentState = ProjectileState.Impacting;
                    }
                    break;
                case ProjectileState.Impacting:
                    break;
                case ProjectileState.ReadyForRecycle:
                    break;
                default:
                    break;
            }
        }

        public enum ProjectileState
        {
            InAir,
            Impacting,
            ReadyForRecycle
        }

        public override Entity Clone()
        {
            Projectile proj = new Projectile();
            proj.CurrentState = this.CurrentState;
            proj.Damage = this.Damage;
            proj.Direction = this.Direction;
            foreach (Debuffs.Effect effect in this.Effects)
            {
                proj.Effects.Add(effect.Clone());
            }
            proj.Name = this.Name;
            proj.Position = this.Position;
            proj.ProjectileSpeed = this.ProjectileSpeed;
            proj.Target = this.Target;
            proj.Height = this.Height;
            proj.Width = this.Width;
            proj.Texture = this.Texture;
            proj.Visible = this.Visible;
            return proj;
        }

        public override void Draw(SpriteBatch sb)
        {
            switch (CurrentState)
            {
                case ProjectileState.InAir:
                    sb.Draw(this.Texture, new Rectangle((int)this.Position.X, (int)this.Position.Y, Width, Height), null, Color.White, this.Direction + (float)Math.PI / 2, new Vector2(Width/2, Height/2), SpriteEffects.None, 0);
                    break;
                case ProjectileState.Impacting:
                    break;
                case ProjectileState.ReadyForRecycle:
                    break;
                default:
                    break;
            }
            
        }
    }
}